import KindList from '@/components/secu/kind-list';
import {
    getExpectCrossFromView,
    getKindsByToken,
    getSecuFromView
} from '@/server/controllers/secuController';

const KindPage = async ({ params }) => {
    const { secuCode } = await params;
    const targetSecu = await getSecuFromView(secuCode);
    if (!targetSecu) return null;
    // const targetTokens = !!targetSecu ? targetSecu.expect.concat(targetSecu.expect_brief) : [];
    // const uniqueTokens = [...new Set(targetTokens)];
    const ztCrossItems = await getExpectCrossFromView();
    const ztCrossNestedTokens = ztCrossItems.map((item) => item.expect_cross);
    const ztCrossTokens = [...new Set(ztCrossNestedTokens.flat(Infinity))];
    const withZtCrossIntersected = targetSecu.tokens.filter(
        (tk) => ztCrossTokens.includes(tk) && !targetSecu.expect_cross.includes(tk)
    );
    // console.log(withZtCrossIntersected);
    let items;
    items = await getKindsByToken(targetSecu.expect_cross);
    if (items.length <= 3) {
        items = await getKindsByToken([...withZtCrossIntersected, ...targetSecu.expect_cross]);
    }
    const zts = items.filter((s) => s.clbn >= 1 && s.secu_code != secuCode) || [];
    const dts = items.filter((s) => s.clbn <= -1 && s.secu_code != secuCode) || [];
    return (
        <>
            <div style={{ borderBottom: '1px dashed', fontSize: '0.5rem', color: 'red' }}>
                {withZtCrossIntersected.join('；')}
            </div>
            <div style={{ borderBottom: '1px dashed', fontSize: '0.5rem', color: 'cyan' }}>
                {targetSecu.expect_cross.join('；')}
            </div>
            <div
                style={{
                    display: 'flex',
                    fontSize: '0.5rem',
                    width: '100%'
                }}
            >
                <KindList items={zts} />
                <KindList items={dts} style={{ color: 'lightgreen' }} />
            </div>
        </>
    );
};

export default KindPage;
